package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.AssessFeedbackBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.UrlAddress;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ProjectName: YJHCloud
 * PackageName: com.yjhcloud.stroke.activitys
 * Description:
 * Create by Ray.
 * Date: 2016/9/23 17:16
 */
public class MyFollowDetailActivity extends BaseActivity implements View.OnClickListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
//    private TextView tv_complete;

    private TextView tv_name;
    private TextView tv_date;
    private LinearLayout ll_scores;//俩圈
    private LinearLayout ll_score1;
    private LinearLayout ll_score1c;
    private TextView tv_score1num;
    private LinearLayout ll_score2;
    private LinearLayout ll_score2c;
    private TextView tv_score2num;
    private View view_scores;
    private LinearLayout ll_report;//评估报告
    private TextView tv_report;
    private LinearLayout ll_nursingquestion;//护理问题
    private TextView tv_nursingquestion;
    private LinearLayout ll_nursingfunctionn;//护理措施，医护
    private TextView tv_nursingfunctionn;
    private LinearLayout ll_nursingfunctionp;//护理措施，患者
    private TextView tv_nursingfunctionp;
    private LinearLayout ll_nursingadd;//添加护理措施
    private LinearLayout ll_moredetail;//了解详情

    private String assessId;
    //    private String assessName;
//    private String assessDate;
    private String patientId;
//    private String patientName;

    private String questionUserId;

    private AssessFeedbackBean feedback;

    private WebView webView;
    private String loadUrl;

    private boolean isShow = false;

    private String isFromRecordList = "";

    private TextView addTV;

    private LinearLayout ll_all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assessfeedback);

        Intent iGet = getIntent();
//        assessId = iGet.getStringExtra("assessId");
//        assessName = iGet.getStringExtra("assessName");
//        assessDate = iGet.getStringExtra("assessDate");
//        patientId = loginInfo.getPatientId();
//        patientName = iGet.getStringExtra("patientName");
        questionUserId = iGet.getStringExtra("questionUserId");

//        if (getIntent().hasExtra("isFromRecordList")) {
//            isFromRecordList = getIntent().getStringExtra("isFromRecordList");
//        }

        initView();
//        initData();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETASSESSFEEDBACKUSER_SUCCESS:
                feedback = (AssessFeedbackBean) msg.obj;
                showData();
                ll_all.setVisibility(View.VISIBLE);
                break;
            case MessageCode.NETWORKCODE_GETASSESSFEEDBACKUSER_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void initView() {

        ll_all = (LinearLayout) findViewById(R.id.ll_assessfeedback_all);

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
//        tv_complete = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText("评估反馈");
//        if ("true".equals(isFromRecordList)) {
//            tv_complete.setVisibility(View.GONE);
//        } else {
//            tv_complete.setVisibility(View.VISIBLE);
//        }

//        tv_complete.setText(getResources().getText(R.string.complete));
        iv_back.setOnClickListener(this);
//        tv_complete.setOnClickListener(this);

        tv_name = (TextView) findViewById(R.id.tv_assessfeedback_title);
        tv_date = (TextView) findViewById(R.id.tv_assessfeedback_date);
        ll_scores = (LinearLayout) findViewById(R.id.ll_assessfeedback_scores);
        ll_score1 = (LinearLayout) findViewById(R.id.ll_assessfeedback_score1);
        ll_score1c = (LinearLayout) findViewById(R.id.ll_assessfeedback_score1c);
        tv_score1num = (TextView) findViewById(R.id.tv_assessfeedback_score1num);
        ll_score2 = (LinearLayout) findViewById(R.id.ll_assessfeedback_score2);
        ll_score2c = (LinearLayout) findViewById(R.id.ll_assessfeedback_score2c);
        tv_score2num = (TextView) findViewById(R.id.tv_assessfeedback_score2num);
        view_scores = findViewById(R.id.view_assessfeedback_scores);
        ll_report = (LinearLayout) findViewById(R.id.ll_assessfeedback_report);//评估报告
        tv_report = (TextView) findViewById(R.id.tv_assessfeedback_report);
        ll_nursingquestion = (LinearLayout) findViewById(R.id.ll_assessfeedback_nursingquestion);//护理问题
        tv_nursingquestion = (TextView) findViewById(R.id.tv_assessfeedback_nursingquestion);
        ll_nursingfunctionn = (LinearLayout) findViewById(R.id.ll_assessfeedback_nursingfunctionn);//护理措施，医护
        tv_nursingfunctionn = (TextView) findViewById(R.id.tv_assessfeedback_nursingfunctionn);
        ll_nursingfunctionp = (LinearLayout) findViewById(R.id.ll_assessfeedback_nursingfunctionp);//护理措施，患者
        tv_nursingfunctionp = (TextView) findViewById(R.id.tv_assessfeedback_nursingfunctionp);
        ll_nursingadd = (LinearLayout) findViewById(R.id.ll_assessfeedback_nursingadd);//添加护理措施
        ll_moredetail = (LinearLayout) findViewById(R.id.ll_assessfeedback_moredetail);
        addTV = (TextView) findViewById(R.id.tv_assessfeedback_nursingadd);

        LinearLayout.LayoutParams lp_circle = new LinearLayout.LayoutParams(screenWidth / 4, screenWidth / 4);
        lp_circle.gravity = Gravity.CENTER;
        ll_score1c.setLayoutParams(lp_circle);
        ll_score2c.setLayoutParams(lp_circle);


        webView = (WebView) findViewById(R.id.detail_webview);

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true); //支持js
        webSettings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSettings.setSupportZoom(true);  //支持缩放
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置缓存模式
        webSettings.setDomStorageEnabled(true);

        loadUrl = UrlAddress.H5_QUESTIONASSESS_URL + "&isEdit=2" + "&questionId=" + assessId + "&questionUserId=" + questionUserId + "&userId=" + loginInfo.getUserId() + "&acctType=" + loginInfo.getUserStyle();
        webView.loadUrl(loadUrl);
//        webView.loadUrl("http://m.baidu.com");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                toastMsg("loadfinish" + view.getContentHeight() + ":" + view.getHeight());
//                System.out.println("height:" + view.getContentHeight());
//                view.getLayoutParams().height = view.getHeight();

//                LinearLayout.LayoutParams lp_wv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, view.getHeight());
//                lp_wv.setMargins(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(10, mContext));
//                view.setLayoutParams(lp_wv);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("patientInfo")) {
                    int hIndex = url.indexOf("pageH=");
                    int wIndex = url.indexOf("pageW=");
                    String heightString = url.substring(hIndex + 6);
                    String widthString = url.substring(wIndex + 6, hIndex - 1);
                    System.out.println("h5w:" + widthString + ";h5h:" + heightString);
                    if (widthString.equals("0")) {
                        view.loadUrl(loadUrl);
                        return true;
                    }
                    int height = (screenWidth - ImageUtil.dip2px(20, mContext)) * (Integer.valueOf(heightString)) / (Integer.valueOf(widthString));
                    LinearLayout.LayoutParams lp_wv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height + 20);
                    lp_wv.setMargins(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(10, mContext));
                    view.setLayoutParams(lp_wv);
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.getAssessFeedbackUser(questionUserId, handler);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void showData() {
        if (feedback != null) {
            String patientTime = "";
            if (feedback.getPatientName() != null) {
                tv_title.setText(feedback.getPatientName() + "的评估反馈");
            } else {
                tv_title.setText("评估反馈");
            }

            tv_name.setText(feedback.getAssessName());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = "";
            if (feedback.getAssessDate() != null) {
                date = sdf.format(new Date(Long.valueOf(feedback.getAssessDate())));
            }

            tv_date.setText(date);

            ll_scores.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(feedback.getScore())) {
                tv_score1num.setText(feedback.getScore());
                ll_score1.setVisibility(View.VISIBLE);
            } else {
                ll_score1.setVisibility(View.GONE);
                view_scores.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(feedback.getLevel())) {
                tv_score2num.setText(feedback.getLevel());
                ll_score2.setVisibility(View.VISIBLE);
            } else {
                ll_score2.setVisibility(View.GONE);
                view_scores.setVisibility(View.GONE);
            }
            if ((TextUtils.isEmpty(feedback.getScore())) && (TextUtils.isEmpty(feedback.getLevel()))) {
                ll_scores.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(feedback.getAssessReport())) {
                ll_report.setVisibility(View.VISIBLE);

                if (feedback.getAssessReport().contains(":")) {
                    SpannableStringBuilder style = new SpannableStringBuilder(feedback.getAssessReport());
                    int end = feedback.getAssessReport().split(":")[0].length() + 1;
                    style.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.black_text)), 0, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                    tv_report.setText(style);
                } else if (feedback.getAssessReport().contains("：")) {
                    SpannableStringBuilder style = new SpannableStringBuilder(feedback.getAssessReport());
                    int end = feedback.getAssessReport().split("：")[0].length() + 1;
                    style.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.black_text)), 0, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                    tv_report.setText(style);
                } else {
                    tv_report.setText(feedback.getAssessReport());
                }
            } else {
                ll_report.setVisibility(View.GONE);
            }

//            if (feedback.getNursingQuestion().size() > 0) {
//                ll_nursingquestion.setVisibility(View.VISIBLE);
//                StringBuffer s = new StringBuffer();
//                for (int i = 0; i < feedback.getNursingQuestion().size(); i++) {
//                    s.append(feedback.getNursingQuestion().get(i));
//                }
//                tv_nursingquestion.setText(s.toString());
//            } else {
//                ll_nursingquestion.setVisibility(View.GONE);
//            }
//
//            if (feedback.getNursingFunctionN().size() > 0) {
//                ll_nursingfunctionn.setVisibility(View.VISIBLE);sdf
//                StringBuffer s = new StringBuffer();
//                for (int i = 0; i < feedback.getNursingFunctionN().size(); i++) {
//                    s.append(feedback.getNursingFunctionN().get(i));
//                }
//                tv_nursingfunctionn.setText(s.toString());
//            } else {
//                ll_nursingfunctionn.setVisibility(View.GONE);
//            }
//
//            if (feedback.getNursingFunctionP().size() > 0) {
//                ll_nursingfunctionp.setVisibility(View.VISIBLE);
//                StringBuffer s = new StringBuffer();
//                for (int i = 0; i < feedback.getNursingFunctionP().size(); i++) {
//                    s.append(feedback.getNursingFunctionP().get(i));
//                }
//                tv_nursingfunctionp.setText(s.toString());
//            } else {
//                ll_nursingfunctionp.setVisibility(View.GONE);
//            }
//
//            if ("true".equals(isFromRecordList)) {
//                addTV.setVisibility(View.GONE);
//                ll_nursingadd.setEnabled(false);
//            }
//
////            if ((!TextUtils.isEmpty(feedback.getNursingFunctionN())) || (!TextUtils.isEmpty(feedback.getNursingFunctionP()))) {
////                ll_nursingadd.setVisibility(View.GONE);
////            } else {
////                ll_nursingadd.setVisibility(View.VISIBLE);
////            }
//            if (!TextUtils.isEmpty(feedback.getHaveAssess())) {
//                if (feedback.getHaveAssess().equals("0")) {
//                    ll_nursingadd.setVisibility(View.GONE);
//                }
//                if (feedback.getHaveAssess().equals("1")) {
//                    ll_nursingadd.setVisibility(View.VISIBLE);
//                }
//            }
//
//            ll_nursingadd.setOnClickListener(this);

            ll_nursingquestion.setVisibility(View.GONE);
            ll_nursingfunctionn.setVisibility(View.GONE);
            ll_nursingfunctionp.setVisibility(View.GONE);
            ll_nursingadd.setVisibility(View.GONE);

            ll_moredetail.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
//            case R.id.tv_title_func:
                finish();
                break;
            case R.id.ll_assessfeedback_nursingadd:
                //添加护理措施
                Intent intent1 = new Intent(this, NursingFunctionActivity.class);
                intent1.putExtra("url", UrlAddress.H5_NURSINGFUNCTION_URL + "&isEdit=0" + "&patientId=" + patientId);
                intent1.putExtra("title", feedback.getAssessName());
                intent1.putExtra("questionUserId", questionUserId);
                startActivity(intent1);

                break;
            case R.id.ll_assessfeedback_moredetail:
                if (isShow) {
                    isShow = false;
                    webView.setVisibility(View.GONE);
                } else {
                    isShow = true;
                    webView.setVisibility(View.VISIBLE);
                }

                break;
        }
    }
}
